const app = {
  middlewares : []
}
app.use = function (fn) {
  app.middlewares.push(fn);
}
app.compose = function () {
  const dispatch = function (i) {
    if (i === app.middlewares.length) return Promise.resolve();
    let fn = app.middlewares[i];
    return Promise.resolve(fn(() => dispatch(i + 1)));
  }
  dispatch(0);
}

app.compose = function () {
  const dispatch = function (i) {
    if (i === app.middlewares.length) return Promise.resolve();
    let fn = app.middlewares[i];
    let res = fn(function() {
      let next = dispatch(i + 1);
      return next;
    });
    return Promise.resolve(res);
  }
  dispatch(0);
}
module.exports = app;
